package com.keller.seckill.controller;

import com.keller.seckill.pojo.User;
import com.keller.seckill.service.GoodsService;
import com.keller.seckill.vo.GoodsVo;
import com.keller.seckill.vo.RespBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.context.WebContext;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

@RequestMapping("/goods")
@Controller
public class GoodsController {

    @Autowired
    private GoodsService goodsService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private ThymeleafViewResolver thymeleafViewResolver;

    @RequestMapping(value = "/toList", produces = "text/html;charset=utf-8")
    @ResponseBody
    public String toList(Model model, User user,
                         HttpServletRequest request, HttpServletResponse response){
        ValueOperations valueOperations = redisTemplate.opsForValue();
        String goodsListHtml = (String)valueOperations.get("goodsList");
        if(!StringUtils.isEmpty(goodsListHtml)){
            return goodsListHtml;
        }
        model.addAttribute("user",user);
        List<GoodsVo>  goodsList = goodsService.findGoodsVo();
        model.addAttribute("goodsList", goodsList);

        WebContext webContext = new WebContext(request, response, request.getServletContext(), request.getLocale(), model.asMap());
        String html = thymeleafViewResolver.getTemplateEngine().process("goodsList", webContext);
        valueOperations.set("goodsList", html);
        return html;
    }

    @RequestMapping("/detail/{goodsId}")
    public String toDetail(User user,Model model, @PathVariable Long goodsId){
        model.addAttribute("user",user);
        GoodsVo  goodsVo = goodsService.findGoodsVoDetailByGoodid(goodsId);
        int seckillStatus = 0;
        int remainSeconds = 0;
        Date beginTime = goodsVo.getStartDate();
        Date endTime = goodsVo.getEndDate();
        Date now = new Date();
        if(now.after(endTime)){
            // 秒杀已结束
            seckillStatus = 2;
            remainSeconds = -1;
        }else if(now.before(beginTime)){
            // 秒杀未开始
            seckillStatus = 0;
            remainSeconds = ((int) ((beginTime.getTime() - now.getTime()) / 1000));
        }else{
            // 秒杀中
            seckillStatus = 1;
        }
        model.addAttribute("seckillStatus", seckillStatus);
        model.addAttribute("user", user);
        model.addAttribute("goods", goodsVo);
        model.addAttribute("remainSeconds", remainSeconds);
        return "goodsDetail";
    }
}
